/**
 * @file imageservices.h
 * @brief
 *
 *
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_CTRLS_IMAGESERVICES_H_
#define MYBOOK_CTRLS_IMAGESERVICES_H_

#include "utils\skin\myimage.h"

static BOOL MyDrawImage(const CMyImage* pImage, HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight)
{
	ATLASSERT(hDestDC != NULL);
	ATLASSERT(nDestWidth > 0);
	ATLASSERT(nDestHeight > 0);
	ATLASSERT(nSrcWidth > 0);
	ATLASSERT(nSrcHeight > 0);

	return pImage->Draw(hDestDC, xDest, yDest, nDestWidth, nDestHeight, xSrc, ySrc, nSrcWidth, nSrcHeight);
}

static void MyDrawImagePart(const HDC &dc, const RECT &rcDest, const CMyImage &image, int iCurrentFrame, int iTotalFrame)
{
	ATLASSERT(image.GetWidth() % iTotalFrame == 0);

	int iWidth = image.GetWidth() / iTotalFrame;

	ATLASSERT(rcDest.right - rcDest.left == iWidth);
	ATLASSERT(rcDest.bottom - rcDest.top == image.GetHeight());

	int iLeft = iWidth * iCurrentFrame;
	RECT rcSrc = { iLeft, 0, iLeft + iWidth, image.GetHeight() };
	MyDrawImage(&image, dc, rcDest.left, rcDest.top, rcDest.right - rcDest.left, rcDest.bottom - rcDest.top, rcSrc.left, rcSrc.top, rcSrc.right - rcSrc.left, rcSrc.bottom - rcSrc.top);
}

static void MyDrawWindowBack(HDC hDstDC, const RECT& rcDest, const CMyImage* pImage)
{
	ATLASSERT(hDstDC != NULL);
	ATLASSERT(pImage != NULL);

	int nDstWidth = rcDest.right - rcDest.left;
	int nDstHeight = rcDest.bottom - rcDest.top;
	int nImgWidth = pImage->GetWidth();
	int nImgHeight = pImage->GetHeight();

// 	ATLASSERT(nDstWidth >= nImgWidth);
// 	ATLASSERT(nDstHeight >= nImgHeight);

	int nHalfImgWidth = nImgWidth / 2;
	int nHalfImgHeight = nImgHeight / 2;

	RECT rcImgPart;
	RECT rcDstPart;

	::SetRect(&rcImgPart, 0, 0, nHalfImgWidth, nHalfImgHeight);
	::SetRect(&rcDstPart, 0, 0, nHalfImgWidth, nHalfImgHeight);
	pImage->Draw(hDstDC, rcDstPart, rcImgPart);

	if (nDstWidth > nHalfImgWidth)
	{
		::SetRect(&rcImgPart, nImgWidth - nHalfImgWidth, 0, nImgWidth, nHalfImgHeight);
		::SetRect(&rcDstPart, nDstWidth - nHalfImgWidth, 0, nDstWidth, nHalfImgHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);
	}

	if (nDstHeight > nHalfImgHeight)
	{
		::SetRect(&rcImgPart, 0, nImgHeight - nHalfImgHeight, nHalfImgWidth, nImgHeight);
		::SetRect(&rcDstPart, 0, nDstHeight - nHalfImgHeight, nHalfImgWidth, nDstHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);
	}

	if (nDstWidth > nHalfImgWidth && nDstHeight > nHalfImgHeight)
	{
		::SetRect(&rcImgPart, nImgWidth - nHalfImgWidth, nImgHeight - nHalfImgHeight, nImgWidth, nImgHeight);
		::SetRect(&rcDstPart, nDstWidth - nHalfImgWidth, nDstHeight - nHalfImgHeight, nDstWidth, nDstHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);
	}

	if (nDstWidth > nImgWidth)
	{
		::SetRect(&rcImgPart, nHalfImgWidth, 0, nHalfImgWidth + 1, nHalfImgHeight);
		::SetRect(&rcDstPart, nHalfImgWidth, 0, nDstWidth - nHalfImgWidth, nHalfImgHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);

		::SetRect(&rcImgPart, nHalfImgWidth, nImgHeight - nHalfImgHeight, nHalfImgWidth + 1, nImgHeight);
		::SetRect(&rcDstPart, nHalfImgWidth, nDstHeight - nHalfImgHeight, nDstWidth - nHalfImgWidth, nDstHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);
	}

	if (nDstHeight > nImgHeight)
	{
		::SetRect(&rcImgPart, 0, nHalfImgHeight, nHalfImgWidth, nHalfImgHeight + 1);
		::SetRect(&rcDstPart, 0, nHalfImgHeight, nHalfImgWidth, nDstHeight - nHalfImgHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);

		::SetRect(&rcImgPart, nImgWidth - nHalfImgWidth, nHalfImgHeight, nImgWidth, nHalfImgHeight + 1);
		::SetRect(&rcDstPart, nDstWidth - nHalfImgWidth, nHalfImgHeight, nDstWidth, nDstHeight - nHalfImgHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);
	}

	if (nDstWidth > nImgWidth && nDstHeight > nImgHeight)
	{
		::SetRect(&rcImgPart, nHalfImgWidth, nHalfImgHeight, nHalfImgWidth + 1, nHalfImgHeight + 1);
		::SetRect(&rcDstPart, nHalfImgWidth, nHalfImgHeight, nDstWidth - nHalfImgWidth, nDstHeight - nHalfImgHeight);
		pImage->Draw(hDstDC, rcDstPart, rcImgPart);
	}
}

static void MyDrawWindowTitle(HDC hDstDC, const RECT& rcDest, LPCTSTR lpszTitle, HFONT hFont)
{
	CDCHandle dc(hDstDC);

	dc.SetTextColor(RGB(255, 255, 255));
	dc.SetBkMode(TRANSPARENT);
	dc.SelectFont(hFont);

	CRect rcDraw(rcDest);
	rcDraw.OffsetRect(10, 1);

	dc.DrawText(lpszTitle, _tcslen(lpszTitle), &rcDraw, DT_SINGLELINE | DT_VCENTER);
}

//////////////////////////////////////////////////////////////////////////
// CImageServices

class CImageServices
{
public:
	static CImageServices* getInstance();

public:
	CImageServices();
	CString GetImagePath(CString strImageName);

protected:
	CString m_strSkinDir;
};

#define __SF(x) (CImageServices::getInstance()->GetImagePath(x))

#endif // MYBOOK_CTRLS_IMAGESERVICES_H_